#!/bin/sh

INTERACTIVE=True

#定义地址
config_path='/tmp/tools'

#是否一键安装
IS_AKEY=0

THIS_PATH=$(cd `dirname $0`; pwd)

#格式化echo 
format_echo(){
	if [ $2 ];then
		echo "\033[31m${1}\033[0m"
	else
		echo "\033[92m${1}\033[0m"
	fi
}

# 设置菜单大小
calc_wt_size() {
  WT_HEIGHT=16
  WT_WIDTH=50
  WT_MENU_HEIGHT=$(($WT_HEIGHT-7))
}
calc_wt_size

#开始接收输入
start(){
	FUN=$(whiptail --title "自美系统开发配置工具（直接选中回车）" --menu "Setup Options" $WT_HEIGHT $WT_WIDTH $WT_MENU_HEIGHT --cancel-button 取消 --ok-button 确定 \
		"1" "一键切换到开发者模式" \
		"2" "打开系统调试日志打印功能" \
		"3" "打开/关闭桌面任务栏" \
		"4" "打开VNC功能" \
		"5" "安装Sqlite图形化管理软件" \
        "6" "启用root帐户登录SSH" \
        "X" "退出" \
        3>&1 1>&2 2>&3)

	case $FUN in
		1)
	    	#format_echo "一键切换到开发者模式"
	    	akey_setup
	    ;;
		2)
	    	#format_echo "打开系统调试日志打印功能"
	    	open_printlog
	    ;;
	    3)
	    	#format_echo "打开/关闭桌面任务栏"
	    	open_taskbar
	    ;;
		4)
	    	#format_echo "打开VNC功能"
	    	enable_vnc
	    ;;
	    5)
	    	#format_echo "安装Sqlite图形化管理软件"
	    	setup_sqlite
	    ;;
	    6)
	    	#format_echo "启用root登录SSH权限"
	    	set_rootbashrc
	    ;;
	    "x"|"X")
	    	format_echo '成功退出！' 1
	    ;;
	    *)
	    	format_echo '成功退出！' 2
	    ;;
	esac
}

#初始化设置
init_set(){
	sudo chmod +x ${config_path}/config/cmd.py
}
init_set

#打开系统调试日志打印功能
open_printlog(){
	if [ $IS_AKEY -eq 0 ]; then
		if [ "$INTERACTIVE" = True ]; then
			whiptail --yes-button "打开" --no-button "关闭" --yesno "是否打开/关闭调试日志打印功能?" $DEFAULT 12 40 2
			RET=$?
		else
			RET=$1
		fi
	else
		RET=0
	fi

	if [ $RET -eq 0 ]; then
		sudo ${config_path}/config/cmd.py openlog
		if [ $IS_AKEY -eq 0 ]; then
			whiptail --title "自美系统" --msgbox "打开调试日志打印功能成功" 10 60
		fi
	elif [ $RET -eq 1 ]; then
		sudo ${config_path}/config/cmd.py closelog
		if [ $IS_AKEY -eq 0 ]; then
			whiptail --title "自美系统" --msgbox "已经关闭调试日志打印功能" 10 60
		fi
	fi
	
	if [ $IS_AKEY -eq 0 ]; then start; fi
}

#打开桌面任务栏
open_taskbar(){
	if [ $IS_AKEY -eq 0 ]; then
		if [ "$INTERACTIVE" = True ]; then
			whiptail --yes-button "打开" --no-button "关闭" --yesno "是否打开/关闭桌面任务栏?" $DEFAULT 12 40 2
			RET=$?
		else
			RET=$1
		fi
	else
		RET=0
	fi

  	if [ $RET -eq 0 ]; then
    	#format_echo "打开任务栏"
    	sudo rm -f /home/pi/.config/lxpanel/LXDE-pi/panels/*

		processId=`ps -ef | grep lxpanel | grep -v grep | awk '{print $2}'`
		sudo kill -9 ${processId}

		if [ $IS_AKEY -eq 0 ]; then
			whiptail --title "自美系统" --msgbox "打开任务栏成功" 10 60
		fi
  	elif [ $RET -eq 1 ]; then
    	#format_echo "关闭任务栏"
    	sudo mv /home/pi/.config/lxpanel/LXDE-pi/panels/panel /home/pi/.config/lxpanel/LXDE-pi/panel_bak
		sleep 1
		sudo mkdir -p /home/pi/.config/lxpanel/LXDE-pi/panels
		sleep 1
		sudo cp -f ${config_path}/config/panel /home/pi/.config/lxpanel/LXDE-pi/panels/panel

		processId=`ps -ef | grep lxpanel | grep -v grep | awk '{print $2}'`
		sudo kill -9 ${processId}

		if [ $IS_AKEY -eq 0 ]; then
			whiptail --title "自美系统" --msgbox "已经关闭任务栏" 10 60
		fi
  	fi

	if [ $IS_AKEY -eq 0 ]; then start; fi
}

#获取VNC状态
#1 -- 没有开启 / 0 -- 已开启
get_vnc() {
  if systemctl status vncserver-x11-serviced.service | grep -q inactive; then
  	echo 1
  else
  	echo 0
  fi
}

is_ssh() {
  if pstree -p | egrep --quiet --extended-regexp ".*sshd.*\($$\)"; then
    return 0
  else
    return 1
  fi
}

#获取SSH状态
#1 -- 没有开启 / 0 -- 已开启
get_ssh() {
  if service ssh status | grep -q inactive; then
    echo 1
  else
    echo 0
  fi
}

#打开VNC功能
enable_vnc(){
	#format_echo "启用VNC远程桌面"
	if [ $IS_AKEY -eq 0 ]; then
		DEFAULT=--defaultno
		if [ $(get_vnc) -eq 0 ]; then
		    DEFAULT=
		fi
		if [ "$INTERACTIVE" = True ]; then
			whiptail --yes-button "启用" --no-button "禁用" --yesno "您希望启用VNC服务吗?" $DEFAULT 20 60 2
			RET=$?
		else
			RET=$1
		fi
	else
		RET=0
	fi
	
	if [ $RET -eq 0 ]; then
		if [ $(get_vnc) -eq 1 ]; then
			if [ ! -d /usr/share/doc/realvnc-vnc-server ] ; then
		    	sudo apt-get install realvnc-vnc-server
			fi
			sudo systemctl enable vncserver-x11-serviced.service
			sudo systemctl start vncserver-x11-serviced.service
		fi
		STATUS='启用'
	elif [ $RET -eq 1 ]; then
		if [ $(get_vnc) -eq 0 ]; then
			sudo systemctl disable vncserver-x11-serviced.service
			sudo systemctl stop vncserver-x11-serviced.service
		fi
		STATUS='禁用'
	else
		return $RET
	fi
	
	if [ "$INTERACTIVE" = True ]; then
		whiptail --msgbox "VNC服务 已$STATUS" 20 60 1
	fi

	if [ $IS_AKEY -eq 0 ]; then start; fi
}

#安装Sqlite
setup_sqlite(){
	if [ ! -d /usr/share/doc/sqlitebrowser ] ; then
		sudo dpkg -i ${config_path}/config/libqscintilla2-qt5-l10n_*.deb
		sudo dpkg -i ${config_path}/config/libqscintilla2-qt5-13_*.deb
		sudo dpkg -i ${config_path}/config/libqt5test5_*.deb	
		sudo dpkg -i ${config_path}/config/sqlitebrowser_*.deb
	fi

	if [ $IS_AKEY -eq 0 ]; then
		whiptail --title "自美系统" --msgbox "安装图形化Sqlite管理工具成功" 10 60
		start;
	fi
}

#启用root登录SSH权限
set_rootbashrc(){
	format_echo "开始启用root登录SSH权限"
	format_echo "修改root账号登录密码（安全考虑建议修改）"
	sudo passwd root

	sudo passwd --unlock root
	
	sudo mv /root/.bashrc /root/.bashrc_bak
	sleep 1
	sudo cp -f ${config_path}/config/.bashrc /root/.bashrc

	sudo ${config_path}/config/cmd.py setroot

	sudo ssh-keygen -A
	sudo systemctl enable ssh
	sudo systemctl start ssh

	IP_ADDR=$(sudo ${config_path}/config/cmd.py getip)

	whiptail --title "自美系统" --msgbox "启用root登录SSH权限成功\nIP：$IP_ADDR\n端口：21232\n账号：root\n密码：刚刚设置的" 12 60
	
	if [ $IS_AKEY -eq 0 ]; then start; fi
}

#一键安装全部环境
akey_setup(){
	IS_AKEY=1
	RET=0
	open_printlog
	open_taskbar
	enable_vnc
	setup_sqlite
	set_rootbashrc
	IS_AKEY=0
	whiptail --infobox "一键切换到开发者模式成功，系统3秒钟后重启。" 8 60
	sleep 3
	sudo reboot
}

start
